package com.ITArmy.service.impl;


import com.ITArmy.beans.Serialize;
import com.ITArmy.beans.UploadUtil;
import com.ITArmy.beans.User;
import com.ITArmy.common.ServerResponse;
import com.ITArmy.dao.BlogMapper;
import com.ITArmy.dao.ChapterMapper;
import com.ITArmy.dao.SerializeMapper;
import com.ITArmy.service.SerializeService;
import com.ITArmy.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class SerializeServiceImpl implements SerializeService {
    @Autowired
    private SerializeMapper serializeMapper;
    @Autowired
    ChapterMapper chapterMapper;
    @Autowired
    BlogMapper blogMapper;
    GetUserSession getUserSession = new GetUserSession();

    //新增连载
    @Override
    public ServerResponse addSerialize(Serialize serialize) {
        int resultCount = serializeMapper.insertSelective(serialize);
        CheckMessage check = new CheckMessage();
        return check.getMessage(resultCount, "添加成功", "添加失败");
    }

    //遍历连载
    @Override
    public List<Serialize> findAllSerialize() {
        List<Serialize> serializes = serializeMapper.findAllSerialize();
        return serializes;
    }

    //删除连载
    @Override
    public ServerResponse deleteSerialize(Long id) {
        int resultCount = serializeMapper.deleteByPrimaryKey(id);
        CheckMessage check = new CheckMessage();
        return check.getMessage(resultCount, "删除成功", "删除失败");
    }

    //批量删除
    @Override
    public ServerResponse deleteManyByIds(String[] ids) {
        int resultCount = serializeMapper.deleteManyByIds(ids);
        CheckMessage check = new CheckMessage();
        return check.getMessage(resultCount, "删除成功", "删除失败");
    }

    //根据id查找连载信息
    @Override
    public ServerResponse findAllSerializeById(Long id) {
        return ServerResponse.createBySuccess(serializeMapper.selectByPrimaryKey(id));
    }

    //根据连载标题模糊搜索
    @Override
    public ServerResponse findAllSerializeByTitle(String title) {
        List<Serialize> serializes = serializeMapper.findAllSerializeByTitle(title);
        if (serializes != null) {
            return ServerResponse.createBySuccess(serializes);
        } else {
            return ServerResponse.createByErrorMesage("无相关信息");
        }
    }

    //根据连载类型搜索
    @Override
    public ServerResponse findAllSerializeByType(Integer tid) {
        List<Serialize> serializes = serializeMapper.findAllSerializeByType(tid);
        if (serializes != null) {
            return ServerResponse.createBySuccess(serializes);
        } else {
            return ServerResponse.createByErrorMesage("无相关信息");
        }
    }

    //修改连载信息
    @Override
    public ServerResponse updateSerialize(Serialize serialize) {
        int resultCount = serializeMapper.updateByPrimaryKeySelective(serialize);
        CheckMessage check = new CheckMessage();
        return check.getMessage(resultCount, "修改成功", "修改失败");
    }

    //是否设置为推荐
    @Override
    public ServerResponse recommend(Long id, Long number) {
        int resultCount = serializeMapper.recommend(id, number);
        CheckMessage check = new CheckMessage();
        return check.getMessage(resultCount, "设置成功", "设置失败");
    }

    //是否上架
    @Override
    public ServerResponse putaway(Long id, Long number, String email) {
        int resultCount = serializeMapper.putaway(id, number);
        if (resultCount == 1) {
            serializeMapper.removeContribute(id);
            if (number == 1) {
                SendEmail sendEmail = new SendEmail();
                String msg = "恭喜！您的投稿已通过审核！";
                sendEmail.sendEmailOther(email, msg);
            } else if (number == 1) {
                SendEmail sendEmail = new SendEmail();
                String msg = "抱歉！您的投稿没有通过审核！";
                sendEmail.sendEmailOther(email, msg);
            }
        }
        CheckMessage check = new CheckMessage();
        return check.getMessage(resultCount, "设置成功", "设置失败");
    }

    //遍历推荐连载
    @Override
    public List<Serialize> recommendList() {
        return serializeMapper.recommendList();
    }

    /**
     * 遍历收费连载
     */
    @Override
    public List<Serialize> putawayList() {
        return serializeMapper.putawayList();

    }

    //根据连载类型遍历推荐连载
    @Override
    public ServerResponse findRecommendByType(Integer tid) {
        List<Serialize> serializes = serializeMapper.findRecommendByType(tid);
        if (serializes != null) {
            return ServerResponse.createBySuccess(serializes);
        } else {
            return ServerResponse.createByErrorMesage("无相关信息...");
        }
    }

    //用户创建新的连载
    @Override
    public ServerResponse setUpSerialize(HttpServletRequest request, Serialize serialize) {
        User user = getUserSession.getUser(request);
        serialize.setuId(user.getuId());
        serialize.setsTime(new Date());
        int resultCount = serializeMapper.insertSelective(serialize);
        CheckMessage check = new CheckMessage();
        return check.getMessage(resultCount, "创建成功", "创建失败");
    }

    //上传连载的封面的图片

    @Override
    public ServerResponse uploadCoverPicture(HttpServletRequest request, long sId, MultipartFile multiUploadFile) {
        Serialize serialize = new Serialize();
        serialize.setsId(sId);
        System.out.println("连载的id为：" + serialize.getsId());
        UploadPicture uploadPicture = new UploadPicture();
        String upPath = "/static/uploads/serializePicture/";
        pictureChange pictureChange = new pictureChange();

        ServerResponse serverResponse = uploadPicture.upload(multiUploadFile, request, upPath);
        if (serverResponse.getData() == null) {
            return ServerResponse.createByErrorMesage("上传失败...");
        }
        //将图片的具体信息储存到这个对象中
        serialize.setsAddress((String) serverResponse.getData());
        //进行上传连载的信息
        serialize.setsTime(new Date());
        System.out.println("图片的信息为：" + serialize.getsAddress());

        int resultCount = serializeMapper.updateByPrimaryKeySelective(serialize);
        if (resultCount == 0) {
            return ServerResponse.createByErrorMesage("上传失败");
        }
        return ServerResponse.createBySuccess("上传成功....", serialize);
    }

    /**
     * @param request   request
     * @param serialize serialize
     * @return
     */
    @Override
    public ServerResponse wirteSerializre(HttpServletRequest request, Serialize serialize) {

        if (serialize.getsTitle() == null || serialize.getsIntroduction() == null) {
            return ServerResponse.createByErrorMesage("请将信息填写完整...");
        }

        User user = getUserSession.getUser(request);
        serialize.setuId(user.getuId());
        serialize.setsTime(new Date());
        serialize.setsContribute("0");
        int resultCount = serializeMapper.insertSelective(serialize);
        if (resultCount > 0) {
            return ServerResponse.createBySuccess("创建成功...", serializeMapper.getNewSerialize(user.getuId()).getsId());
        } else {
            return ServerResponse.createByErrorMesage("创建失败...");
        }
    }

    //用户删除连载的所有的内容
    @Override
    public ServerResponse deleteByUser(long serializeId) {
        int resultCount = serializeMapper.deleteByPrimaryKey(serializeId);
        int ResultCount2 = chapterMapper.deleteAllChapter(serializeId);
        if (ResultCount2 != 0 && resultCount != 0) {
            return ServerResponse.createBySuccessMessage("删除成功...");
        }
        return ServerResponse.createByErrorMesage("删除失败...");
    }

    /**
     * 获取用户所发表过的所有连载
     */
    @Override
    public List<Serialize> getAllPublicSerialize(HttpServletRequest request) {
        List<Serialize> serializes = serializeMapper.getPublicSerialize(getUserSession.getUser(request).getuId());
        Collections.reverse(serializes);
        return serializes;
    }

    /**
     * Base64上传图片
     */
    @Override
    public ServerResponse Base64Upload(HttpServletRequest request, UploadUtil uploadUtil) {
        if (uploadUtil.getsId() == 0) {
            uploadUtil.setsId(serializeMapper.getNewSerialize(getUserSession.getUser(request).getuId()).getsId());
        }
        pictureChange pictureChange = new pictureChange();
        String path = "/static/uploads/serializePicture/";
        //meiyoushenem
        ServerResponse serverResponse = pictureChange.uploadHeader(uploadUtil.getBase(), request, path);
        Serialize serialize = new Serialize();
        serialize.setsTime(new Date());
        serialize.setsId(uploadUtil.getsId());
        System.out.println(uploadUtil.getsId());
        if (!serverResponse.isSuccess()) {
            return ServerResponse.createByErrorMesage("上传失败...");
        }
        serialize.setsAddress((String) serverResponse.getData());
        int result = serializeMapper.updateByPrimaryKeySelective(serialize);
        if (result > 0) {
            return ServerResponse.createBySuccessMessage("上传成功...");
        } else {
            return ServerResponse.createByErrorMesage("上传失败...");
        }
    }

    /**
     * 遍历投稿连载
     *
     * @return
     */
    @Override
    public List<Serialize> findContributeSerialize() {
        return serializeMapper.findContributeSerialize();
    }

    /**
     * 通过标题查找投稿连载
     *
     * @param title title
     * @return ServerResponse
     */
    @Override
    public ServerResponse findContributeByTitle(String title) {
        List<Serialize> serializes = serializeMapper.findContributeByTitle(title);
        if (serializes != null) {
            return ServerResponse.createBySuccess(serializes);
        } else {
            return ServerResponse.createByErrorMesage("无相关信息...");
        }
    }

    /**
     * 根据投稿连载类型搜索
     */
    @Override
    public ServerResponse findContributeByType(Integer tid) {
        List<Serialize> serializes = serializeMapper.findContributeByType(tid);
        if (serializes != null) {
            return ServerResponse.createBySuccess(serializes);
        } else {
            return ServerResponse.createByErrorMesage("无相关信息...");
        }
    }


}
